<template>
  <div>
    <!--
      上传的服务器地址
      action="#"
      上传的文件列表
      :file-list="fileList"
      文件列表的类型
      list-type="picture-card"
      动态显示上传的框框
      :class="{disable:fileList.length>0}"
      预览
      :on-preview="handlePreview"
      删除事件
      :on-remove="onRemove"
      改变事件
      :on-change="onChange"
      覆盖原来的上传行为
      :http-request="upload"
      上传之前 判断大小和格式
      :before-upload="beforeUpload"
      -->
    <el-upload
      action="#"
      :file-list="fileList"
      list-type="picture-card"
      :class="{ disable: fileList.length > 0 }"
      :on-preview="handlePreview"
      :on-remove="onRemove"
      :on-change="onChange"
      :http-request="upload"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus avatar-uploader-icon" />
    </el-upload>

    <el-progress
      v-if="progressShow"
      :percentage="percent"
      :show-text="false"
      style="width: 146px"
    />

    <el-dialog title="预览" :visible="dialogShow" @close="dialogShow = false">
      <el-row type="flex" justify="center">
        <img :src="imgsrc" alt="">
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID96nEPJwINBizyh9gwwf7ICqDKAj13nGS',
  SecretKey: 'xe4D5i6D3dw4vVoqc7LJ0QZR3OSFkzyg'
})
export default {
  data() {
    return {
      fileList: [
        // {
        //   name: '小白',
        //   url: 'https://img0.baidu.com/it/u=1443138647,880212012&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=571'
        // }
      ],
      imgsrc: '',
      dialogShow: false,
      //   进度条
      percent: 0,
      progressShow: false
    }
  },
  methods: {
    handlePreview(img) {
      this.dialogShow = true
      this.imgsrc = img.url
    },
    onRemove(file, fileList) {
      this.fileList = [...fileList]
    },
    onChange(file, fileList) {
      this.fileList = [...fileList]
    },
    beforeUpload(file) {
      console.log('上传前', file)
      const whiteList = ['image/jpeg', 'image/png']
      if (file.size > 200 * 1024) {
        this.$message.error('图片不能大于200k')
        return false
      }
      if (whiteList.indexOf(file.type) === -1) {
        this.$message.error('暂时只支持jpg或png')
        return false
      }
    },
    upload(data) {
      this.progressShow = true
      console.log(data)
      cos.putObject(
        {
          Bucket: 'heima-renzi-1307117927' /* 必须 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: data.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: data.file, // 上传文件对象
          onProgress: (progressData) => {
            console.log(JSON.stringify(progressData))
            this.percent = progressData.percent * 100
          }
        },
        (err, data) => {
          console.log(err || data)
          this.fileList[0].url = `http://${data.Location}`
          this.fileList[0].status = 'success'
          setTimeout(() => {
            this.progressShow = false
          }, 800)
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.disable {
  // /deep/ .el-upload--picture-card {
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}
</style>
